<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href="/ordermaker/pages/layui/css/layui.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">

    #include("common.html")

    <div class="layui-body">
        <div style="margin-left: 10px;margin-right: 10px;margin-top: 15px;">
            <table class="layui-hide" id="test" lay-filter="test"></table>
        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="change">改变状态</a>
</script>
<script src="/ordermaker/pages/layui/layui.js"></script>
<script>
    //JavaScript代码区域
    layui.use(['element','table'], function(){
        var element = layui.element;
        var table = layui.table;

        var tableIns = table.render({
            elem: '#test'
            ,url:'/ordermaker/admin/myrecord/list'
            ,title: '录课记录表'
            ,cols: [[
                {field:'id', title:'ID', fixed: 'left'}
                ,{field:'name', title:'使用者'}
                ,{field:'date', title:'使用时间'}
                ,{field:'xingqi', title:'星期'}
                ,{field:'jieci', title:'节次'}
                ,{field:'sheying', title:'录课老师'}
                ,{field:'st', title: '数据状态', width: 200
                    ,templet: function(d){
                        return '<span style="color: red;">'+d.st+'</span>'
                    }
                }
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,parseData: function(res){ //res 即为原始返回的数据
                console.log(res)
                return {
                    "code": res.erro, //解析接口状态
                    "count": res.datas.totalRow, //解析数据长度
                    "data": res.datas.list //解析数据列表
                };
            }
            ,page: true
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'change'){
                layui.jquery.ajax({
                    url: "/ordermaker/admin/myrecord/changestate",
                    type: 'post',//method请求方式，get或者post
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    xhrFields:{
                        withCredentials:true
                    },
                    dataType: 'json',//预期服务器返回的数据类型
                    data: {id:data.id},//表格数据序列化
                    success: function (res) {//res为相应体,function为回调函数
                        console.log(res);
                        layer.msg(data.name+"的数据状态已改变");
                        tableIns.reload();
                        layer.close(index);
                    },
                    error: function (res) {
                        console.log(res);
                        layer.alert('操作失败！！！', {icon: 5});
                    }
                });
            }
        });
    });
</script>
</body>
</html>